package com.lzz.log;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");

        try {
            String script = "function validate(input) { return (input.a); }";
            engine.eval(script);

            if (engine instanceof javax.script.Invocable) {
                javax.script.Invocable invocableEngine = (javax.script.Invocable) engine;
                Map<String , Object> map = new HashMap<>();
                map.put("a","23");
                String valid = (String) invocableEngine.invokeFunction("validate", map);
                System.out.println("Is valid: " + valid);
            }
        } catch (ScriptException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
